#include <string.h>
#include "reclog.h"

using namespace log4cxx;
using namespace log4cxx::helpers;

RecLog4C::RecLog4C()
{
	_logger = log4cxx::Logger::getLogger("global");
}

RecLog4C::~RecLog4C()
{
}

bool RecLog4C::init(const char* logfile,const char* logname)
{
	try{
		PropertyConfigurator::configure(logfile);
	}
	catch(std::exception& e){
		LOG4CXX_FATAL(_logger,e.what());
		return false;
	}
	if (logname != NULL && strlen(logname) != 0){
		_logger = log4cxx::Logger::getLogger(logname);
	}
	return true;
}

void RecLog4C::fatal(const std::string& msg)
{
	_logger->fatal(msg);
}

void RecLog4C::warn(const std::string& msg)
{
	_logger->warn(msg);
}

void RecLog4C::info(const std::string& msg)
{
	_logger->info(msg);
}

void RecLog4C::debug(const std::string& msg)
{
	_logger->debug(msg);
}

void RecLog4C::trace(const std::string& msg)
{
	_logger->trace(msg);
}


RecLogFactory RecLogFactory::_factory;

RecLogFactory::RecLogFactory()
{
	_isDefaultLog = true;
	_glog = new RecLog4C();
}

RecLogFactory::~RecLogFactory()
{
	if(_isDefaultLog){
		delete _glog;
	}
}

void RecLogFactory::setGlobalLog(RecLogI *log)
{
	if (log != NULL){
		_factory._isDefaultLog = false;
		_factory._glog = log;
	}
}

RecLogI* RecLogFactory::getGlobalLog()
{
	return _factory._glog;
}

